//打开单选人员的选择框
function openKeywordSelectWindow(window, url, idJsvar, nameJsvar) {
	var para = new Array(3);
	para[0] = window;
	para[1] = idJsvar;
	para[2] = nameJsvar;
	showModalDialog(url, para, 'dialogWidth:300px;dialogHeight:380px;');
}
// 打开多选人员的选择框
function openMultiSelectWindow(window, url, idJsvar, nameJsvar) {
	var para = new Array(3);
	para[0] = window;
	para[1] = idJsvar;
	para[2] = nameJsvar;
	showModalDialog(url, para, 'dialogWidth:470px;dialogHeight:380px;');
}
// 打开共享范围的选择框
function openShareRegionWindow(window, url, showFrame) {
	var para = new Array(2);
	para[0] = window;
	para[1] = showFrame;
	showModalDialog(url, para,
			'dialogWidth:400px;dialogHeight:420px;scroll:no;');
}

function initSelectedOption(idJsvar, nameJsvar) {
	// alert('aaa');
	if (idJsvar) {
		initOption(getIdField(), idJsvar.getValue().split(","));
	}
	if (nameJsvar) {
		initOption(getValueField(), nameJsvar.getValue().split(","));
	}
}
// 初始化已经选中的option
function initOption(option, value) {
	// alert(option+':'+value);
	for ( var i = 0; i < value.length; i++) {
		var idValue = value[i];
		if (idValue != "") {
			var newIdOption = document.createElement("OPTION");
			newIdOption.setAttribute("value", idValue);
			newIdOption.text = idValue;
			option.options.add(newIdOption);
		}
	}
}
// 将选中项加入到已选中列表中
function addValueToSelectOption(selctObject, value) {
	var isDup = false;
	for ( var i = 0; i < selctObject.length; i++) {
		var opValue = selctObject.options[i].value;
		if (opValue == value) {
			isDup = true;
		}
	}
	if (!isDup) {
		var newValueOption = document.createElement("OPTION");
		newValueOption.setAttribute("value", value);
		newValueOption.text = value;
		selctObject.options.add(newValueOption);
	}
}
// 选中项加入到已选列表中前进行判断,根据id来判断才正确
function addValueToSelectOptionForCheck(selctObject, value, checkObject,
		checkId) {
	var isDup = false;
	for ( var i = 0; i < checkObject.length; i++) {
		var opValue = checkObject.options[i].value;
		if (opValue == checkId) {
			isDup = true;
		}
	}
	if (!isDup) {
		var newValueOption = document.createElement("OPTION");
		newValueOption.setAttribute("value", value);
		newValueOption.text = value;
		selctObject.options.add(newValueOption);
	}
}
// 删除选中的域
function removeSelectedOption() {
	var idOption = getIdField();
	var valueOption = getValueField();
	var selectedIndex = valueOption.selectedIndex;
	if (selectedIndex == -1) {
		alert("必须选择");
		return;
	} else {
		idOption.remove(selectedIndex);
		valueOption.remove(selectedIndex);
	}
}
// 删除全部选中内容
function removeAll() {
	var idOption = getIdField();
	var valueOption = getValueField();
	idOption.options.length = 0;
	valueOption.options.length = 0;
}
// 点击确定后，填充到父页面
function fillSelectedOption(idJsvar, nameJsvar) {
	var idOption = getIdField();
	var valueOption = getValueField();
	if (idJsvar)
		idJsvar.setValue(convertSelectOptionToString(idOption));
	if (nameJsvar)
		nameJsvar.setValue(convertSelectOptionToString(valueOption));
	window.close();
}
// 选中左边的全部到右边
function moveAll() {

}
// 将选中域进行上移
function moveUp() {
	var idOption = getIdField();
	var valueOption = getValueField();
	var selectedIndex = valueOption.selectedIndex;
	// alert(selectedIndex);
	if (selectedIndex == -1) {
		// alert("必须选择!");
		return;
	} else if (selectedIndex == 0) {
		// alert("已经到最上方!");
		return;
	} else {
		exchangeUp(valueOption, selectedIndex);
		exchangeUp(idOption, selectedIndex);
	}
}
// 将选中域进行下移
function moveDown() {
	var idOption = getIdField();
	var valueOption = getValueField();
	var selectedIndex = valueOption.selectedIndex;
	// alert(selectedIndex);
	if (selectedIndex == -1) {
		// alert("必须选择!");
		return;
	} else if (selectedIndex == valueOption.length - 1) {
		// alert("已经到最下方!");
		return;
	} else {
		exchangeDown(valueOption, selectedIndex);
		exchangeDown(idOption, selectedIndex);
	}
}
function moveToRight(selectedNode, idJsvar, nameJsvar) {
	// alert('aaabbb');
	var idField = getIdField();
	var valueField = getValueField();
	if (selectedNode && selectedNode != null) {
		var firstChild = selectedNode.firstChild;
		if (firstChild == null)
			return;
		if (firstChild.attributes.expanded) {
			if (idJsvar)
				addValueToSelectOption(idField,
						firstChild.attributes.hrefTarget);
			if (nameJsvar)
				addValueToSelectOption(valueField, firstChild.text);
		}
		var next = firstChild.nextSibling;
		while (next != null) {
			if (!next.attributes.expanded) {
				break;
			} else {
				if (idJsvar)
					addValueToSelectOption(idField, next.attributes.hrefTarget);
				if (nameJsvar)
					addValueToSelectOption(valueField, next.text);
			}
			next = next.nextSibling;
		}
		selectedNode = null;
	}
}
function exchangeUp(myOption, selectedIndex) {
	var sValue = myOption.options[selectedIndex].value;
	var sText = myOption.options[selectedIndex].text
	var preIndex = selectedIndex - 1;
	var preValue = myOption.options[preIndex].value;
	var preText = myOption.options[preIndex].value;
	myOption.options[preIndex].value = sValue;
	myOption.options[preIndex].text = sText;
	myOption.options[selectedIndex].value = preValue;
	myOption.options[selectedIndex].text = preText;
	myOption.selectedIndex = selectedIndex - 1;
}
function exchangeDown(myOption, selectedIndex) {
	var sValue = myOption.options[selectedIndex].value;
	var sText = myOption.options[selectedIndex].text
	var preIndex = selectedIndex + 1;
	var preValue = myOption.options[preIndex].value;
	var preText = myOption.options[preIndex].value;
	myOption.options[preIndex].value = sValue;
	myOption.options[preIndex].text = sText;
	myOption.options[selectedIndex].value = preValue;
	myOption.options[selectedIndex].text = preText;
	myOption.selectedIndex = selectedIndex + 1;
}
// 将option转换成逗号隔开的String
function convertSelectOptionToString(selectObject) {
	var fieldValue = "";
	for ( var i = 0; i < selectObject.options.length; i++) {
		fieldValue = fieldValue + selectObject.options[i].value + ",";
	}
	if (fieldValue.length > 1) {
		return fieldValue.substring(0, fieldValue.length - 1);
	} else {
		return "";
	}
}
function getIdField() {
	return document.getElementById("selectedId");
}

function getValueField() {
	return document.getElementById("selectedValue");
}
function clearSelect(idJsvar, nameJsvar) {
	if (idJsvar) {
		idJsvar.setValue('');
	}
	if (nameJsvar) {
		nameJsvar.setValue('');
	}
}